#include	"unp.h"

typedef struct CountInfo {
	uint32_t byte_count;
	uint32_t word_count;
	uint32_t line_count;
}_CountInfo;

void
send_words(FILE *fp, int sockfd)
{
	char	sendline[MAXLINE], recvline[MAXLINE];
	_CountInfo countInfo;

	countInfo.byte_count = 0;
	countInfo.line_count = 0;
	countInfo.word_count = 0;

	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		Writen(sockfd, sendline, strlen(sendline));
		if(strlen(sendline) == 2 && sendline[0]=='.'){

			Readline(sockfd, &countInfo, MAXLINE);

			err_msg("------ result -------");
			err_msg("  byte count : %d", countInfo.byte_count);
			err_msg("  word count : %d", countInfo.word_count);
			err_msg("  line count : %d", countInfo.line_count);
			err_msg("---------------------");
			err_quit("Client terminate");
		}

		/*
		if (Readline(sockfd, recvline, MAXLINE) == 0)
			err_quit("str_cli: server terminated prematurely");

		Fputs(recvline, stdout);
		 */
	}
}


int
main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	err_msg("Client start!");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	send_words(stdin, sockfd);		/* do it all */

	exit(0);
}
